package com.vision.admin.sv.serv.rpc;

import java.util.List;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;

import com.vision.core.cm.action.CreateUserResult;
import com.vision.core.cm.db.data.User;

/**
 * 
 * @author Mark
 *
 */
@Aspect
public class UserPasswordStripper implements Ordered {
	
	private int order;

	@Override
	public int getOrder() {
		return order;
	}
	
	public void setOrder(int order) {
		this.order = order;
	}
	
	public void stripSingle(User user) {
		if (user != null) {
			user.setPassword(null);
		}
	}
	
	/**
	 * XXX: Ideally the parameter should be List<User> but I can't figure out
	 * how to specify that in the AOP configuration (see admin-context.xml), thus
	 * this workaround for now. 
	 * 
	 * @param list
	 */
	@SuppressWarnings("unchecked")
	public void stripMulti(Object list) {
		if (list instanceof List) {
			List<User> usersList = (List<User>) list;
			for (User user : usersList) {
				user.setPassword(null);
			}
		}
	}
	
	public void stripCreateUserResult(CreateUserResult result) {
		if (result != null) {
			result.getUser().setPassword(null);
		}
	}

}
